﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using CommonFramework.Utility;

namespace Back.Xinkui.Common.Control.Region
{
    /// <summary>
    /// 国家 省份 城市
    /// </summary>
    public class ApsotoRPC : WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            if (Page.IsPostBack)
            {
                SelectCountry = WebHelper.GetFormHtmlValue<int>("selectCountry", 0);
                SelectProvince = WebHelper.GetFormHtmlValue<int>("selectProvince", 0);
                SelectCity = WebHelper.GetFormHtmlValue<int>("selectCity", 0);
            }
            base.OnLoad(e);
        }

        protected override void Render(HtmlTextWriter output)
        {
            StringBuilder sbCountry = new StringBuilder(@"<option value=""0"">--Country--</option>");
            foreach (var country in RegionManager.GetCountry())
            {
                if (SelectCountry == country.CountryID)
                {
                    sbCountry.AppendLine(string.Format(@"<option value=""{0}"" selected=""selected"">{1}</option>", country.CountryID, country.Name));
                }
                else
                {
                    sbCountry.AppendLine(string.Format(@"<option value=""{0}"">{1}</option>", country.CountryID, country.Name));
                }
            }
            output.Write(@"<select name=""selectCountry"" id=""selectCountry"" onchange=""initProvince(this)"" class=""{1}"">{0}</select>", sbCountry.ToString(), this.CssClass);

            StringBuilder sbProvince = new StringBuilder(@"<option value=""0"">--Province--</option>");
            if (SelectCountry > 0)
            {
                foreach (var p in RegionManager.GetProvinceByCountry(SelectCountry))
                {
                    if (SelectProvince == p.ProvinceID)
                    {
                        sbProvince.AppendLine(string.Format(@"<option value=""{0}"" selected=""selected"">{1}</option>", p.ProvinceID, p.Province));
                    }
                    else
                    {
                        sbProvince.AppendLine(string.Format(@"<option value=""{0}"">{1}</option>", p.ProvinceID, p.Province));
                    }
                }
            }
            output.Write(@"<select name=""selectProvince"" id=""selectProvince"" onchange=""initCity(this)"" class=""{1}"">{0}</select>", sbProvince.ToString(), this.CssClass);


            StringBuilder sbCity = new StringBuilder(@"<option value=""0"">--City--</option>");
            if (SelectProvince > 0)
            {
                foreach (CityEntity c in RegionManager.GetCityByProvince(SelectProvince))
                {
                    if (SelectCity == c.CityID)
                    {
                        sbCity.AppendLine(string.Format(@"<option value=""{0}"" selected=""selected"">{1}</option>", c.CityID, c.City));
                    }
                    else
                    {
                        sbCity.AppendLine(string.Format(@"<option value=""{0}"">{1}</option>", c.CityID, c.City));
                    }
                }
            }
            output.Write(@"<select name=""selectCity"" id=""selectCity"" class=""{1}"">{0}</select>", sbCity.ToString(), this.CssClass);

            //output.WriteLine(@"<script type=""text/javascript"">");
            //output.WriteLine(@"function initCity(obj) {");
            //output.WriteLine(@"$(""#selectCity"").empty();");
            //output.WriteLine(@"$(""#selectCity"").append(""<option value='0'>城市</option>"");");
            //output.WriteLine(@"$(""#selectDistrict"").empty();");
            //output.WriteLine(@"$(""#selectDistrict"").hide();");
            //output.WriteLine(@"$.getJSON(""/b2c/handlers/RegionHandler.ashx"" + ""?t=c&v="" + obj.value, function(data) {");
            //output.WriteLine(@"$.each(data.contents, function(i, item) {");
            //output.WriteLine(@"$(""#selectCity"").append(""<option value='"" + item.Key + ""'>"" + item.Value + ""</option>"");");
            //output.WriteLine(@"});");
            //output.WriteLine(@"$(""#selectDistrict"").show();");
            //output.WriteLine(@"$(""#selectDistrict"").append(""<option value='0'>区县</option>"");");
            //output.WriteLine(@"});");
            //output.WriteLine(@"}");

            //output.WriteLine(@"function initDistrict(obj) {");
            //output.WriteLine(@"$(""#selectDistrict"").empty();");
            //output.WriteLine(@"$(""#selectDistrict"").append(""<option value='0'>区县</option>"");");
            //output.WriteLine(@"$.getJSON(""/b2c/handlers/RegionHandler.ashx"" + ""?t=d&v="" + obj.value, function(data) {");
            //output.WriteLine(@"$.each(data.contents, function(i, item) {");
            //output.WriteLine(@"$(""#selectDistrict"").append(""<option value='"" + item.Key + ""'>"" + item.Value + ""</option>"");");
            //output.WriteLine(@"});");
            //output.WriteLine(@"});");
            //output.WriteLine(@"}");

            //output.WriteLine(@"function initRegion(ProvinceID,CityID,DistrictID)");
            //output.WriteLine(@"{");
            //output.WriteLine(@"$.ajaxSettings.async = false;");
            //output.WriteLine(@"if(ProvinceID)");
            //output.WriteLine(@"{");
            //output.WriteLine(@"$(""#selectProvince"").val(ProvinceID);");
            //output.WriteLine(@"var objPro=document.getElementById(""selectProvince"").options[window.document.getElementById(""selectProvince"").selectedIndex];");
            //output.WriteLine(@"initCity(objPro);");
            //output.WriteLine(@"}");
            //output.WriteLine(@"if(CityID)");
            //output.WriteLine(@"{");
            //output.WriteLine(@"$(""#selectCity"").val(CityID);");
            //output.WriteLine(@"var objCity=document.getElementById(""selectCity"").options[window.document.getElementById(""selectCity"").selectedIndex];");
            //output.WriteLine(@"initDistrict(objCity)");
            //output.WriteLine(@"}");
            //output.WriteLine(@"if(DistrictID)");
            //output.WriteLine(@"{");
            //output.WriteLine(@"$(""#selectDistrict"").val(DistrictID)");
            //output.WriteLine(@"}");
            //output.WriteLine(@"$.ajaxSettings.async = true");
            //output.WriteLine(@"}");
            //output.WriteLine(@"</script>");


        }

        [Category("RPC Protroy")]
        [Browsable(true)]
        [Description("选择的省份")]
        public int SelectProvince
        { get; set; }

        [Category("RPC Protroy")]
        [Browsable(true)]
        [Description("选择的城市")]
        public int SelectCity
        { get; set; }

        [Category("RPC Protroy")]
        [Browsable(true)]
        [Description("选择的国家")]
        public int SelectCountry
        { get; set; }
    }
}
